﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace healthdemo
{
    public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
            InitData();
        }
        //初始化数据
        private void InitData()
        {
            string[] lstStr = { "男", "女"};
            SexBox.Items.AddRange(lstStr);
            SexBox.SelectedItem = "男";

            //体检类型
            string[] list = { "A型", "B型", "C型" };
            PakageBox.Items.AddRange(list);
            PakageBox.SelectedItem = "A型";

            DateTime curDate = DateTime.Now;
            DateBox.Value = new DateTime(curDate.Year, 1, 1);

            //体检状态
            DataTable dtLeiBie = new HealthExaminationSys.BLL.sys_item().GetList("itemType ='RYZT'").Tables[0];
            StatusBox.DataSource = dtLeiBie;
            StatusBox.DisplayMember = "itemText";
            StatusBox.ValueMember = "itemCode";
        }

        HealthExaminationSys.BLL.userinformation userinformation = new HealthExaminationSys.BLL.userinformation();

        //添加
        private void childAdd_Click(object sender, EventArgs e)
        {
            HealthExaminationSys.Model.userinformation user = new HealthExaminationSys.Model.userinformation();

            bool val = validate();
            if (val)
            {
                user.name = NameBox.Text;

                user.sex = SexBox.Text;

                user.age = int.Parse(AgeBox.Text);

                user.idcard = IdCardBox.Text;

                user.date = Convert.ToDateTime(DateBox.Text);

                user.tel = TelBox.Text;

                user.pakage = PakageBox.Text;

                user.status = StatusBox.Text;

                user.adress = AdressBox.Text;

                if (userinformation.Add(user))
                {

                    MessageBox.Show("添加成功！");
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("添加失败!");
                }
            }
            else {
                MessageBox.Show("请完善资料!");
            }

            
        }

        //验证是否电话号码和手机号码 
        public static bool IsPhoneNo(string str_handset)
        {
            return Regex.IsMatch(str_handset, "^(0\\d{2,3}-?\\d{7,8}(-\\d{3,5}){0,1})|(((13[0-9])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9])|(14[0-9]))\\d{8})$");
        }

        //验证身份证号
        public bool IsIDcard(string str_idcard)
        {
            return Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");
        }

        //验证
        public bool validate()
        {
            bool flag = true;

            if (NameBox.Text == "" || NameBox.Text == null)
            {
                flag = false;
            }

            if (SexBox.Text == "" || SexBox.Text == null)
            {
                flag = false;
            }

            if (AgeBox.Text == "" || AgeBox.Text == null)
            {
                flag = false;
            }

            if (IdCardBox.Text == "" || IdCardBox.Text == null || !IsIDcard(IdCardBox.Text))
            {
                flag = false;
            }

            if (DateBox.Text == "" || DateBox.Text == null)
            {
                flag = false;
            }
            if (TelBox.Text == "" || TelBox.Text == null || !IsPhoneNo(TelBox.Text))
            {
                flag = false;
            }
            if (PakageBox.Text == "" || PakageBox.Text == null)
            {
                flag = false;
            }
            if (StatusBox.Text == "" || StatusBox.Text == null)
            {
                flag = false;
            }
            if (AdressBox.Text == "" || AdressBox.Text == null)
            {
                flag = false;
            }

            return flag;
        }

    }
}
